<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.appology.jokes.Joke" %>
<%@ page import="com.appology.users.ChuckUser" %>
<%@ page import="com.appology.users.ChuckUserHelper" %>
<%
	// Get the currently logged in user
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	
	// Set up a couple of variables and make them null
	String name = null;
	ChuckUser chuckUser = null;
	
	// If the user is not logged in, just set the name to Chuck Norris
	if (user == null) { 
		name = "Chuck Norris";
	} else {
		
		// If the user is logged in, see if there is a corresponding ChuckUser object in the datastore
		// If there is, we can use the user's full display name.  Otherwise, we will use their Google username.
	    chuckUser = ChuckUserHelper.getChuckUserByGoogleId(userService.getCurrentUser().getUserId());
		
		if(chuckUser == null){
			name = user.getNickname();
		} else {
			name = chuckUser.getDisplayName();
		}
	}
	
	// Now we'll get a random joke with the name we have set up.
	String joke = Joke.getInstance().getRandom(name);
%>
<!doctype html>
<%@include file='includes/ie-conditionals.html'%>
<head>
  <meta charset="utf-8">


  <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
       Remove this if you use the .htaccess -->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Better than Chuck | Appology</title>
    <!-- Mobile viewport optimized: j.mp/bplateviewport -->
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <!-- Place favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="apple-touch-icon" href="/apple-touch-icon.png">


  <%@include file='includes/styles.html'%>


  <!-- All JavaScript at the bottom, except for Modernizr which enables HTML5 elements & feature detects -->
  <script src="js/libs/modernizr-1.7.min.js"></script>
  </head>

  <body id="main_body">
  <img id="top" src="img/top.png" alt="">
  <div  id="form_container">
    <header>
	<h1><a>Better Than Chuck</a></h1>
    </header>
    <div id="main" role="main">
    <form id="form_194508" class="appnitro"  method="post" action="">
		<div class="form_description">
		
			<% // Show the title as Facts about "name" where "name" is the current user's 
			   // name with Chuck Norris as a fallback. %>
			<h2><img class="logo" src="/img/appology-50.png">Facts about <%= name %></h2>
			
			<% // If the user not logged in, show them a sign in link %>
			<% if (user != null){ %>
			<p>Only the facts, and all about you.</p>
			<% } else { %>
			<p>Chuck Norris is great, but <a href="/auth">sign in</a> to see facts about you.</p>
			<% } %>
			
		</div>		
		
		<% // Display formatted the joke %>
		<p id="joke_text"><%= joke %></p>
			
		<ul >
			<li class="buttons">
			    <input type="hidden" name="form_id" value="194508" />
			    
				<input id="saveForm" class="button_text" type="submit" name="submit" value="Get another fact" />
				
				<% // If the user is logged in, and has granted permission to access Buzz, let them post directly through the app, otherwise use Buzz's JavaScript API so they can post themselves. %>
				<% if(chuckUser != null){ %>
						<input id="post_to_buzz" class="button_text" type="submit" name="post-to-buzz" value="Post to Buzz" />
				<% }else{ %>
						<input id="post_to_buzz_non_user" class="button_text" type="submit" name="post_to_buzz_non_user" value="Post to Buzz" />
				<% } %>
				
				<img class="loader" style="display:none;" src="/img/ajax-loader.gif">
				
			</li>
			<li id="buzz_response"></li>
		</ul>
		
		</form>	
		
		<%@include file='includes/feedcontainer.html'%>
	    <%@include file='includes/emailform.html'%>

    </div>
    
    <footer  id="footer">
    
    	<% // If the user is logged in, let them sign out or email the administrator, 
    	   // otherwise show them a sign in link %>
	    <% if (user != null) {  %>
			<p><a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a> | <a id="email_admin" href="#">email admin</a></p>
		<% } else { %>
			<p><a href="/auth">Sign in</a> to get awesome facts about you.</p>
		<% } %>		
	</footer>
   
  </div> <!--! end of #container -->
  <img id="bottom" src="img/bottom.png" alt=""> 
  
  <%@include file='includes/scripts.html'%>
 
  </body>
</html>
